package com.example.pcmintuidemo;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MyRankingWebViewClient extends WebViewClient {

    private Intent intent;
    private Context context;

    private final static String tag = MyRankingWebViewClient.class.getName();
    public MyRankingWebViewClient(Context context){
        this.context = context;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        System.out.println(url);
        intent = new Intent();
        if(url.contains("http://192.168.22.22:8601/smart_express_vue/#/myRanking")){
            view.loadUrl(url);
        }else if(url.equals("/")){
            intent.setClass(context,MainActivity.class);
            context.startActivity(intent);
        }else if(url.contains("http://fanyi.youdao.com/")){
            Log.i(tag,"url："+url);
            intent.setClass(context,MessageDetailActivity.class);
            context.startActivity(intent);
        }
        //返回值是true的时候WebView打开，为false则系统浏览器或第三方浏览器打开。
        // 如果要下载页面中的游戏或者继续点击网页中的链接进入下一个网页的话，重写此方法下，不然就会跳到手机自带的浏览器了，而不继续在你这个webview里面展现了
        return true;
    }

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);
    }

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
//        super.onReceivedSslError(view,handler,error);
        handler.proceed();
    }
}
